package com.rssreader.client.components;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rssreader.client.RSSReaderConstants;
import com.rssreader.client.lists.FeedList;
import com.rssreader.client.services.FeedServiceAsync;
import com.rssreader.client.windows.FeedWindow;
import com.rssreader.shared.model.Feed;

public class Copy_2_of_RssNavigationPanel extends ContentPanel {
    public Copy_2_of_RssNavigationPanel() {
	setHeading("Navigation");
	setLayout(new FitLayout());
	initToolbar();
	// add a new instance of the FeedList component to the underlying
	// ContentPanel
	add(new FeedList());

	// Menu menu = new Menu();
	//
	// //Create feed
	// final MenuItem miCreateFeed = new MenuItem("Create feed");
	// miCreateFeed.setIconStyle("create-feed");
	//
	// ToolTipConfig createNewToolTipConfig = new ToolTipConfig();
	// createNewToolTipConfig.setTitle("Create a new RSS feed");
	// createNewToolTipConfig.setText("Create a new RSS feed");
	// miCreateFeed.setToolTip(createNewToolTipConfig);
	//
	// //MenuEvent
	// miCreateFeed.addSelectionListener(new SelectionListener<MenuEvent>()
	// {
	// @Override
	// public void componentSelected(MenuEvent ce) {
	// createNewFeedWindow();
	// }
	// });
	//
	// //把create feed按鈕加進menu裡
	// menu.add(miCreateFeed);
	//
	// //Link feed
	// final MenuItem miLinkFeed = new MenuItem("Link feed");
	// miLinkFeed.setIconStyle("link-feed");
	//
	// ToolTipConfig linkFeedToolTipConfig = new ToolTipConfig();
	// linkFeedToolTipConfig.setTitle("Link to existing RSS feed");
	// linkFeedToolTipConfig.setText("Allows you to enter the URL of an existing RSS feed you would like to link to");
	// miLinkFeed.setToolTip(linkFeedToolTipConfig);
	//
	// final LinkFeedPopup linkFeedPopup = new LinkFeedPopup();
	// linkFeedPopup.setConstrainViewport(true); //要保證pop
	// up的彈出始終在整個面板Viewport內
	//
	// miLinkFeed.addSelectionListener(new SelectionListener<MenuEvent>() {
	// @Override
	// public void componentSelected(MenuEvent ce) {
	// linkFeedPopup.show(miLinkFeed.getElement(), "tr?");
	// }
	// });
	//
	// //把link feed按鈕加進menu裡
	// menu.add(miLinkFeed);
	//
	// //menuBar
	// MenuBar menuBar = new MenuBar();
	// MenuBarItem menuBarItem = new MenuBarItem("Add feed", menu);
	// menuBar.add(menuBarItem);
	//
	// //panel的頂端
	// setTopComponent(menuBar);
    }

    @Override
    protected void onRender(Element parent, int pos) {
	super.onRender(parent, pos);
    }

    private void createNewFeedWindow() {
	/*
	 * final Window newFeedWindow = new FeedWindow(new Feed());
	 * newFeedWindow.show();
	 */

	/*
	 * 通過GWT的RPC去調用server端了（FeedServiceImpl）
	 * 首先調用FeedServiceImpl.createNewFeed()方法。 當執行完畢之後，成功了就會回調onSuccess()，
	 * 反之失敗了，就會回調onFailure()方法
	 */
	final FeedServiceAsync feedService = Registry
		.get(RSSReaderConstants.FEED_SERVICE);
	feedService.createNewFeed(new AsyncCallback<Feed>() {
	    @Override
	    public void onSuccess(Feed feed) {
		final Window newFeedWindow = new FeedWindow(feed);
		newFeedWindow.show();
	    }

	    @Override
	    public void onFailure(Throwable caught) {
		Info.display("RSSReader", "Unable to create a new feed");
	    }
	});
    }

    private void initToolbar() {
	final ToolBar toolbar = new ToolBar();

	// 新增一個Add feed按鈕，用於被添加到toolbar裡
	final Button btnAddFeed = new Button("Add feed");
	btnAddFeed.setIconStyle("add-feed");

	ToolTipConfig addFeedToolTipConfig = new ToolTipConfig();
	addFeedToolTipConfig.setTitle("Add a new RSS feed");
	addFeedToolTipConfig.setText("Add a new RSS feed");
	btnAddFeed.setToolTip(addFeedToolTipConfig);

	// 新增一個Menu
	Menu menu = new Menu();

	// 新增一個Create feed按鈕，且定義其ToolTip，添加到menu裡
	final MenuItem miCreateFeed = new MenuItem("Create feed");
	miCreateFeed.setIconStyle("create-feed");

	ToolTipConfig createNewToolTipConfig = new ToolTipConfig();
	createNewToolTipConfig.setTitle("Create a new RSS feed");
	createNewToolTipConfig.setText("Create a new RSS feed");
	miCreateFeed.setToolTip(createNewToolTipConfig);

	// MenuEvent
	miCreateFeed.addSelectionListener(new SelectionListener<MenuEvent>() {
	    @Override
	    public void componentSelected(MenuEvent ce) {
		createNewFeedWindow();
	    }
	});
	menu.add(miCreateFeed);

	// 新增一個Link feed按鈕，定義其ToolTip，添加到menu裡
	final MenuItem miLinkFeed = new MenuItem("Link feed");
	miLinkFeed.setIconStyle("link-feed");

	ToolTipConfig linkFeedToolTipConfig = new ToolTipConfig();
	linkFeedToolTipConfig.setTitle("Link to existing RSS feed");
	linkFeedToolTipConfig
		.setText("Allows you to enter the URL of an existing RSS feed you would like to link to");
	miLinkFeed.setToolTip(linkFeedToolTipConfig);

	final LinkFeedPopup linkFeedPopup = new LinkFeedPopup();
	linkFeedPopup.setConstrainViewport(true); // 要保證pop
						  // up的彈出始終在整個面板Viewport內

	miLinkFeed.addSelectionListener(new SelectionListener<MenuEvent>() {
	    @Override
	    public void componentSelected(MenuEvent ce) {
		linkFeedPopup.show(miLinkFeed.getElement(), "tr?");
	    }
	});
	// 把link feed按鈕加進menu裡
	menu.add(miLinkFeed);

	// set menu into button
	btnAddFeed.setMenu(menu);
	// set button into toolbar
	toolbar.add(btnAddFeed);
	// set toolbar into ContenPanel
	setTopComponent(toolbar);
    }
}
